/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetlif13.model.puissance43D;

import projetlif13.model.Game3D;
import projetlif13.model.Move;
import projetlif13.model.Move3D;
import projetlif13.model.Player;

/**
 *
 * @author theo
 */
public class P43DGame extends Game3D {

    public P43DGame(Player _p1, Player _p2) {
        super(_p1, _p2);
        this.gameBoard = new P43DBoard();
        if (_p1 instanceof IAPlayerP43D) {
            ((IAPlayerP43D) _p1).start();
        }
        if (_p2 instanceof IAPlayerP43D) {
            ((IAPlayerP43D) _p2).start();
        }
    }

    @Override
    public boolean gameEnded() {
        for (int x = 0; x < gameBoard.getWidth(); x++) {
            for (int y = 0; y < gameBoard.getHeight(); y++) {
                for (int z = 0; z < gameBoard.getDepth(); z++) {
                    if (gameBoard.getPIDByXYZ(x, y, z) != -1) {
                        int serieLength;

                        /////////// LIGNES //////////
                        //largeur
                        serieLength = 0;
                        for (int x2 = x; x2 < gameBoard.getWidth(); x2++) {
                            if (gameBoard.getPIDByXYZ(x, y, z) == gameBoard.getPIDByXYZ(x2, y, z)) {
                                serieLength++;
                            } else {
                                break;
                            }
                        }
                        if (serieLength == 4) {
                            return true;
                        }

                        //longueur
                        serieLength = 0;
                        for (int y2 = y; y2 < gameBoard.getWidth(); y2++) {
                            if (gameBoard.getPIDByXYZ(x, y, z) == gameBoard.getPIDByXYZ(x, y2, z)) {
                                serieLength++;
                            } else {
                                break;
                            }
                        }
                        if (serieLength == 4) {
                            return true;
                        }

                        //hauteur
                        serieLength = 0;
                        for (int z2 = z; z2 < gameBoard.getWidth(); z2++) {
                            if (gameBoard.getPIDByXYZ(x, y, z) == gameBoard.getPIDByXYZ(x, y, z2)) {
                                serieLength++;
                            } else {
                                break;
                            }
                        }
                        if (serieLength == 4) {
                            return true;
                        }

                        /////////// DIAGONALES //////////

                        /*
                         * Les || ont été remplacés par des && en effet on
                         * sortait du plateau sinon et on obtenait un
                         * java.lang.ArrayIndexOutOfBoundsException
                         */
                        int x2, y2, z2;

                        //diagonale z(n-1) = z
                        serieLength = 0;
                        x2 = x;
                        y2 = y;
                        z2 = z;
                        while (x2 < gameBoard.getWidth() && y2 < gameBoard.getHeight() && z2 < gameBoard.getDepth()) {

                            if (gameBoard.getPIDByXYZ(x2, y2, z2) == gameBoard.getPIDByXYZ(x2, y2, z2)) {
                                serieLength++;
                            } else {
                                break;
                            }
                            x2++;
                            y2++;
                        }
                        if (serieLength == 4) {
                            return true;
                        }

                        //diagonale y(n-1) = y
                        serieLength = 0;
                        x2 = x;
                        y2 = y;
                        z2 = z;
                        while (x2 < gameBoard.getWidth() && y2 < gameBoard.getHeight() && z2 < gameBoard.getDepth()) {

                            if (gameBoard.getPIDByXYZ(x2, y2, z2) == gameBoard.getPIDByXYZ(x2, y2, z2)) {
                                serieLength++;
                            } else {
                                break;
                            }
                            x2++;
                            z2++;
                        }
                        if (serieLength == 4) {
                            return true;
                        }

                        //diagonale x(n-1) = x
                        serieLength = 0;
                        x2 = x;
                        y2 = y;
                        z2 = z;
                        while (x2 < gameBoard.getWidth() && y2 < gameBoard.getHeight() && z2 < gameBoard.getDepth()) {
                            if (gameBoard.getPIDByXYZ(x2, y2, z2) == gameBoard.getPIDByXYZ(x2, y2, z2)) {
                                serieLength++;
                            } else {
                                break;
                            }
                            y2++;
                            z2++;
                        }
                        if (serieLength == 4) {
                            return true;
                        }

                    }
                }
            }
        }
        return false;
    }

    @Override
    public boolean moveAvailable(Move _m) {
        Move3D move = (Move3D) _m;
        if (gameBoard.getPIDByXYZ(move.x, move.y, move.z) == -1) {
            return true;
        }
        return false;
    }
}
